﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using NewsClassifierFreshnessMeasurement.Model;
using System.Threading;

namespace ProduceConfig
{
    class Program
    {
        public int groupID;
        public List<string> listKey;
        public string filePath;
        List<ConfigEntry> listConfig;
        public Program()
        {
            listKey = new List<string>();
            listConfig = new List<ConfigEntry>();
        }
        public void Start()
        {
            ReadDataFromDatabase();
            ProduceFile();
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("Group ID:");
            p.groupID = Convert.ToInt32(Console.ReadLine());

            while (true)
            {
                Console.WriteLine("Key ('end' to end input):");
                string tmp = Console.ReadLine();
                if (tmp == "end")
                    break;
                p.listKey.Add(tmp);
            }

            Console.WriteLine("Config file path:");
            p.filePath = Console.ReadLine();
            p.Start();
        }
        void ReadDataFromDatabase()
        {
            string connString = "server=lsstc259;database=NewsClassifierFreshMeasurement;uid =freshnessReader;pwd=@freshnessReader@";
            SqlConnection sqlConn = new SqlConnection(connString);
            sqlConn.Open();
            

            foreach (string key in listKey)
            {
                SqlCommand cmd = sqlConn.CreateCommand();
                cmd.CommandText = "SELECT * FROM [Configuration] where [Key] = @key and GroupID=@groupID";
                cmd.Parameters.Add("@key", SqlDbType.NVarChar,512);
                cmd.Parameters["@key"].Value = key;
                cmd.Parameters.Add("@groupID", SqlDbType.Int);
                cmd.Parameters["@groupID"].Value = groupID;
                SqlDataReader r = cmd.ExecuteReader();
                Console.WriteLine(key + r.HasRows);
                while (r.Read())
                {
                    ConfigEntry config = new ConfigEntry()
                    {
                        ID = Convert.ToInt32(r["ID"]),
                        Key = r["Key"].ToString(),
                        Description = r["Description"].ToString(),
                        Value = r["Value"].ToString(),
                        IsValueCollection = Convert.ToBoolean(r["IsValueCollection"])
                    };
                    listConfig.Add(config);
                }
                r.Close();
            }
            sqlConn.Close();
        }
        void ProduceFile()
        {


            FileStream stream = new FileStream(filePath, FileMode.Create);
            XmlTextWriter w = new XmlTextWriter(stream, Encoding.UTF8);
            w.Formatting = Formatting.Indented;
            w.WriteStartDocument();

            w.WriteStartElement("Configuration");
            w.WriteStartElement("ConfigEntries");
            foreach (ConfigEntry config in listConfig)
            {
                w.WriteStartElement("ConfigEntry");
                w.WriteElementString("ID",config.ID.ToString());
                w.WriteElementString("Key", config.Key);
                w.WriteElementString("Value", config.Value);
                w.WriteElementString("IsValueCollection", config.IsValueCollection.ToString().ToLower());
                w.WriteElementString("Description", config.Description);
                w.WriteElementString("Delimiter", config.Delimiter);
                w.WriteEndElement();
            }
            w.WriteEndElement();
            w.WriteEndElement();
            w.WriteEndDocument();
            w.Flush();
            stream.Close();
            Console.WriteLine("Document created.");
        }
    }
}
